package com.xuxueli.applyModules.utils.testcreateword;
import com.google.common.collect.Lists;
import com.xuxueli.applyModules.entity.UserAvatar;
import fr.opensagres.xdocreport.core.document.SyntaxKind;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.images.ByteArrayImageProvider;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import fr.opensagres.xdocreport.template.formatter.NullImageBehaviour;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.util.HtmlUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

/**
 * 根据模板填值及图片
 * 工具栏“插入”->“文档部件或文本”->“域” -> 选择邮件合并，添加对应属性：${age}
 */
@Slf4j
public class TestCreateWord {

    public static void main(String[] args) {
        createXdocreport();
    }

    public static void createXdocreport() {
        try {
            //使用远程模板
            //InputStream inputStream = new URL(templateUrl).openConnection().getInputStream();
            ClassPathResource classPathResource = new ClassPathResource("summarytest.docx");
            File file = classPathResource.getFile();
            FileInputStream inputStream = new FileInputStream(file);
            //FileInputStream inputStream = new FileInputStream(new File("D:\\data\\summarytest.docx"));
            File outputPath = new File("D:\\data\\testtemplete.docx");
            FileOutputStream outputStream = new FileOutputStream(outputPath);
            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(inputStream, TemplateEngineKind.Freemarker);
            // 设置特殊字段
            FieldsMetadata metadata = report.createFieldsMetadata();
            metadata.addFieldAsTextStyling("content", SyntaxKind.Html);
            metadata.addFieldAsImage("avatar", "user.avatar", NullImageBehaviour.RemoveImageTemplate);
            report.setFieldsMetadata(metadata);
            // 创建内容-text为模版中对应都变量名称
            String content = "&#x3c;p&#x3e;我在这里放了一段富文本&#x3c;/p&#x3e;" +
                    "&#x3c;p&#x3e;我准备测试富文本的处理&#x3c;/p&#x3e;";
            content = HtmlUtils.htmlUnescape(content);
            IContext context = report.createContext();
            context.put("name", "年终总结大会");
            context.put("time", "2021年3月26日");
            context.put("place", "线上");
            context.put("sponsor", "张三");
            context.put("content", content);
            //图片这里放图片的输入流
            //InputStream p1 = new FileInputStream(new File("D:\\data\\图片1.png"));
            ClassPathResource classPathResources = new ClassPathResource("11.jpg");
            InputStream p1 = classPathResources.getInputStream();
            ClassPathResource classPathResourcess = new ClassPathResource("图片2.jpg");
            InputStream p2 = classPathResourcess.getInputStream();
            //InputStream p2 = new FileInputStream(new File("D:\\data\\图片2.jpg"));
            List<UserAvatar> users = Lists.newArrayList(
                    new UserAvatar("张三", "组织部", new ByteArrayImageProvider(p1)),
                    new UserAvatar("李四", "宣传部", new ByteArrayImageProvider(p2)));
            context.put("userList", users);
            // 生成文件
            report.process(context, outputStream);
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            log.info("生成纪要文件发生异常：<{}>", e.getMessage());
        }
    }

}
